package com.pafcio.sudoku;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.fest.util.Collections;

public class Vector {

	private Integer[] values = new Integer[9]; 
	
	public Vector(Integer[] values) {

		this.values[0] = values[0]; 
		this.values[1] = values[1]; 
		this.values[2] = values[2]; 
		this.values[3] = values[3]; 
		this.values[4] = values[4]; 
		this.values[5] = values[5]; 
		this.values[6] = values[6]; 
		this.values[7] = values[7]; 
		this.values[8] = values[8]; 
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		for (int i = 0; i < 9; i++) {
			sb.append(values[i]);
		}

		return sb.toString();
	}

	public boolean isOK() {		
		return containsAllDigits();
	}

	private boolean containsAllDigits() {
		Set<Integer> numbersSet = Collections.set(values);
		for(int i=1; i<=9; i++){
			if(!numbersSet.contains(Integer.valueOf(i)))
				return false;
		}
		
		return true;
	}

	public Integer getValueAt(int index) {
		return values[index];
	}

	public Integer[] getValues() {
		return values;
	}

	public void setValueAt(Integer value, int index) {
		values[index] = value;
	}

	public boolean vectorContainsNumber(int i) {
		return Arrays.asList(getValues()).contains(i);
	}

	public Set<Integer> getMissingValues() {
		Set<Integer> missingValues = new HashSet<Integer>();
		for(int i = 1; i <= 9; i++) {
			missingValues.add(i);
		}
		
		missingValues.removeAll(Arrays.asList(getValues()));
		
		return missingValues;
	}

	public Set<Integer> getMissingIndexes() {
		Set<Integer> missingIndex = new HashSet<Integer>();
		for(int i = 1; i <= 9; i++) {
			if(values[i-1] == null)
				missingIndex.add(i-1);
		}
		
		return missingIndex; 
	}
}
